import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:overload_guard/app_theme.dart';
import 'package:overload_guard/pages/change_password_page.dart';
import 'package:overload_guard/pages/change_phone_page.dart';
import 'package:overload_guard/pages/sign_in_page.dart';
import 'package:overload_guard/services/api_service.dart';
import 'package:overload_guard/services/db_service.dart';

class UserCenterPage extends StatefulWidget {
  @override
  State<UserCenterPage> createState() => _UserCenterPageState();
}

class _UserCenterPageState extends State<UserCenterPage> {

  Future<dynamic> _initPage() async{
    // 获取用户信息
    var userInfoResult = await APIService.userQueryById(await DBService.getData(DBKey.userId));

    return userInfoResult;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          title: Text("用户中心", style: TextStyle(color: Colors.white)),
          backgroundColor: AppTheme.primaryColor,
          centerTitle: true,
          leading: IconButton(
            icon: Icon(
              Icons.arrow_back,
              color: Colors.white,
            ),
            onPressed: () {
              Get.back();
            },
          )),
      backgroundColor: Color(0xFFEDEDED),
      body: FutureBuilder(
        future: _initPage(),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            print('加载完成');
            return ListView(
              children: [
                Padding(
                  padding: EdgeInsets.only(top: 10),
                ),
                _infoTile("账号", snapshot.data["phone"], context),
                _infoTile("姓名", snapshot.data["userName"], context),
                _infoTile("车牌号", snapshot.data["carNum"], context),
                _infoTile("所属公司", snapshot.data["company"], context),
                _infoTile("联系电话", snapshot.data["phone"], context),
                Padding(padding: EdgeInsets.only(top: 15)),
                ListTile(
                  title: Text(
                    "修改密码",
                    textAlign: TextAlign.center,
                  ),
                  tileColor: Colors.white,
                  onTap: () => Get.to(ChangePasswordPage()),
                ),
                Padding(padding: EdgeInsets.only(top: 5)),
                ListTile(
                  title: Text("修改电话", textAlign: TextAlign.center),
                  tileColor: Colors.white, onTap: ()=>Get.to(ChangePhonePage()),),
                Padding(padding: EdgeInsets.only(top: 5)),
                ListTile(
                  title: Text(
                    "退出登陆",
                    textAlign: TextAlign.center,
                    style: TextStyle(color: Colors.red),
                  ),
                  tileColor: Colors.white,
                  onTap: () async {
                    await DBService.saveData(DBKey.userSignInStatus, false);
                    Get.offAll(SignInPage());
                  },
                ),
              ],
            );
          } else if (snapshot.hasError) {
            return Text('报错了！');
          } else {
            print('加载中');
            return Center(child: CircularProgressIndicator());
          }
        },
      ) ,
    );
  }

  _infoTile(String text, String value, BuildContext context) {
    return ListTile(
        title: Text(text),
        trailing: Text(
          value,
          style: Theme.of(context).textTheme.labelLarge,
        ),
        tileColor: Colors.white);
  }
}
